// ************************* 78 character ruler *****************************
// PowerupCurrentTest.c -- Implementation of FAHA RFBB 
//
// $Author: Liu Rongwu $
// $Header: /RFBB_TDDSW/FZHA_RFBB/Sources/RFBB/PowerupCurrentTest.c
// $History: 09/25/2010 create
//
// (c) 2008 Nokia Siemens Networks.
// ************************* 78 character ruler *****************************

#include "RFBB.h"
//#include "ICD_PSU.h"
#include <utility.h>
#include <formatio.h>
#include "PsuContorl.h"
#include "USB6501.h" 

_DAQMX_WRITE_PORT_CMD g_szBscanClearWritePort2Cmd = {"BSCAN_SHORT","DIO1/port2",0xff};      

// -----------------------------------------------------------------------
// Function: PowerupCurrentTest  
// Purpose:  switch PSU ON (if selected by parameter) and measure current
//           The purpose of the test is to turn on voltages for DUT and check the current consumption after specified delay.
// Type:     Numeric limit test
// 

void __decldllexp TX_TEST PowerupCurrentTest(tTestData *testData, tTestError *testError)
{
   int error = 0;             // required for tsErrChk
   ErrMsg errMsg = {'\0'};    // required for tsErrChk
   ERRORINFO errorInfo;       // required for tsErrChk
   
   ErrMsg szLogMsg = {'\0'};  
   CAObjHandle hStepName = 0; 
   
   char* pszStepName = 0; 
   char* pszPowerSupply  = 0;    // IDC PSU logical name "PSU0"
   char  szStepName[255] = {'\0'};
   
   double dRes = 999.999;     // default bogus
   double dDelay = 0;         // delay after power up, Testplan
   double dVoltage = 0;       // PSU voltage, Testplan
   double dCurrentLimit = 0;  // PSU currentlimit, Testplan
   double dPostCurrentLimit = 0;  // PSU currentlimit, Testplan
   double dCurrentThre = 0.0;
   double dTimeElapsed = 0.0;
   double dStartTime = 0.0;

//   int iLoop = 0;
   VBOOL vbExists = VARIANT_FALSE;  
   
   //=====================================================================
   
   // Resolve StepName 
   tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                      TS_SeqContextStep, CAVT_OBJHANDLE, 
                                      &hStepName));
   tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
   
   // Write Stepname to eventlog
   sprintf(szLogMsg, "##### %s #####", pszStepName);
   ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg) );   
   
   // Post message to GUI
   PostMessageToGui(pszStepName);
   
   // --------------------------------------------------------------------
   // Get Test Parameters from Sequence
   // --------------------------------------------------------------------
 
   sprintf(szStepName, "%s.%s.PARAMETERS.DELAY", TESTPLAN_BASE_PATH, pszStepName);    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szStepName, 0, &dDelay));
   
   sprintf(szStepName, "%s.%s.PARAMETERS.VOLTAGE", TESTPLAN_BASE_PATH, pszStepName);  
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szStepName, 0, &dVoltage));  

   sprintf(szStepName, "%s.%s.PARAMETERS.CURRENTLIMIT", TESTPLAN_BASE_PATH, pszStepName);  
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szStepName, 0, &dCurrentLimit));  

   sprintf(szStepName, "%s.%s.PARAMETERS.CURRENTHRESHOLD", TESTPLAN_BASE_PATH, pszStepName);  
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szStepName, 0, &dCurrentThre)); 

   sprintf(szStepName, "%s.%s.PARAMETERS.POST_CURRENTLIMIT", TESTPLAN_BASE_PATH, pszStepName);  
   tsErrChk(TS_PropertyExists (testData->seqContextCVI, NULL, szStepName, 0, &vbExists));
   if( vbExists == VARIANT_TRUE ) {
      tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szStepName, 0, &dPostCurrentLimit));  
   }
   else{
      dPostCurrentLimit = dCurrentLimit;
   }   

   sprintf(szStepName, "%s.%s.PARAMETERS.PSULOGICALNAME", TESTPLAN_BASE_PATH, pszStepName);
   tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo,
                            szStepName, 0, &pszPowerSupply));

   
#ifndef NO_PSU

//   ErrChk(PSU_Open (pszPowerSupply));
   ErrChk(PSU_SetVoltage (g_vi_sInitHandleN5700_1, dVoltage));
   ErrChk(PSU_SetCurrent (g_vi_sInitHandleN5700_1, dCurrentLimit));
   ErrChk(PSU_SetOutputState(g_vi_sInitHandleN5700_1, 1));
   
   sprintf(szLogMsg, "Dut PSU power on, The maximum waiting time %.1f seconds..", dDelay);
   ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg) );
   
   //Delay(dDelay); 
   //ErrChk(PSU_MeasureCurrent(pszPowerSupply, &dRes));
   #if 0
   if ( dRes < 1 )
      sprintf(szLogMsg, "Current measurement result is %0.1f mA", dRes*1000);
   else
      sprintf(szLogMsg, "Current measurement result is %0.1f A", dRes);
   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
   #endif        
   
    dStartTime = Timer(); 

    Delay(10);

    ErrChk(PSU_MeasureCurrent(g_vi_sInitHandleN5700_1, &dRes));
    
    while (1)
    {

        ErrChk(PSU_MeasureCurrent(g_vi_sInitHandleN5700_1, &dRes));

        if (dRes > dCurrentThre)
        {
            dTimeElapsed = Timer() - dStartTime;
            sprintf(szLogMsg, "DUT has power on..., it takes about %.1f seconds",dTimeElapsed);
            ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));         
            break;
        }
        else
        {
            ;
        }
        
        Delay(1);
       
        dTimeElapsed = Timer() - dStartTime;	//
        if( dTimeElapsed > dDelay ) 
        {
    	    error = -99.9;
            sprintf(szLogMsg, "PSU0 power on Timeout...");
            ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg)); 
    	    goto Error;
        }    
    }
    
    //ErrChk(PSU_SetCurrent (pszPowerSupply, dPostCurrentLimit));
#endif

Error: 

#if(ON_OFF != SIMU_VERS)
   testData->measurement = dRes;
#else
   testData->measurement = 3.0;
#endif
   // FREE RESOURCES
   CA_FREE( pszStepName );
   CA_FREE( pszPowerSupply );
   CA_DISCARD_OBJ(hStepName);

   // Post message to GUI
   PostMessageToGui("");

   // If an error occurred, set the error flag to cause a run-time error in TestStand.
   if (error < 0){
      testError->errorFlag = TRUE;
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
    
   return;    
}
/**************************************************************************************************
  Function:        BscanPoweroff
  Description:     This test is the tests turn off Power and clear DIO1.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    BscanOverPowerOff
  Used Command:    
**************************************************************************************************/

void __decldllexp TX_TEST BscanPoweroff(tTestData *testData, tTestError *testError)
{    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char szReturnString[BUF_SIZE_32K + 1] = {'\0'};
	char szTemp[512] = {'\0'};
	
    int error = 0;
    int i=0;
    int iPassFail= 1;
    int iReturn=0;
    int iPipeNum = 0;
    int iIQNum = 0;
    unsigned int iShiftBit = 0;
    int iFbNum = 0;
    int iByte = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                               TS_SeqContextStep, CAVT_OBJHANDLE, 
                               &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                         &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                               TS_UIMsg_ProgressText, 0.0,
                               pszStepName, 0, VTRUE));
    
    /******************************************test ***************************************************/
    
    sprintf(szLogMsg,"************BB board Power off***********");
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);

    ErrChk( PSU_SetOutputState( g_vi_sInitHandleN5700_1, 0 ));
    
    //Clear DIO state
    ErrChk(USBWriteDigToPort(g_szBscanClearWritePort2Cmd.ucTashName,g_szBscanClearWritePort2Cmd.ucDevport,g_szBscanClearWritePort2Cmd.uiWriteData));

    Delay(2);
    
Error: 

#if(ON_OFF != SIMU_VERS)

    if(iPassFail == PASS) 
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }
    
    CA_DISCARD_OBJ( hStepName );
	CA_FREE( pszStepName );
   
	//If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
    
#else
    testData->result = PASS;
#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                  TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);
    
    return;
			 
}
   
void __decldllexp TX_TEST BoardPowerOnAndSelfRet(tTestData *testData, tTestError *testError)
{    
   ErrMsg errMsg = {'\0'};
   ERRORINFO errorInfo;
   ErrMsg szLogMsg;
   CAObjHandle hStepName = 0;
   char* pszStepName = NULL;
   char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
   char szReturnString[BUF_SIZE_32K + 1] = {'\0'};
   char szTemp[512] = {'\0'};
   
   int error = 0;
   int i=0;
   int iPassFail= 1;
   int iReturn=0;
   int iPipeNum = 0;
   int iIQNum = 0;
   unsigned int iShiftBit = 0;
   int iFbNum = 0;
   int iByte = 0;
   //////////////////////////////////////////////////////////////////////////   
   // Resolve StepName 
   tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                              TS_SeqContextStep, CAVT_OBJHANDLE, 
                              &hStepName));
   tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                        &pszStepName));

   sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
   ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

   // Post message to GUI
   tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                              TS_UIMsg_ProgressText, 0.0,
                              pszStepName, 0, VTRUE));
   
   /******************************************test ***************************************************/
   
   sprintf(szLogMsg,"************BB board Power On and Reset***********");
   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
   //InitPSU();
   ErrChk(PSU_SetOutputState(g_vi_sInitHandleN5700_1, 1));       
   
   Delay(5);
   
Error: 

#if(ON_OFF != SIMU_VERS)

   if(iPassFail == PASS) 
   {
       testData->result = PASS;
   }
   else 
   {
       testData->result = FAIL;
   }
   
   CA_DISCARD_OBJ( hStepName );
   CA_FREE( pszStepName );
  
   //If an error occurred, set the error flag to cause a run-time error in TestStand.
   if(error < 0)
   {
       testError->errorFlag = TRUE;
       testError->errorCode = error;
       testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
   
#else
   testData->result = PASS;
#endif
   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);
   
   return;
            
}
void __decldllexp TX_TEST BscanPoweroffAndOn(tTestData *testData, tTestError *testError)
{    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char szReturnString[BUF_SIZE_32K + 1] = {'\0'};
	char szTemp[512] = {'\0'};
	
    int error = 0;
    int i=0;
    int iPassFail= 1;
    int iReturn=0;
    int iPipeNum = 0;
    int iIQNum = 0;
    unsigned int iShiftBit = 0;
    int iFbNum = 0;
    int iByte = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                               TS_SeqContextStep, CAVT_OBJHANDLE, 
                               &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                         &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                               TS_UIMsg_ProgressText, 0.0,
                               pszStepName, 0, VTRUE));
    
    /******************************************test ***************************************************/
    
    sprintf(szLogMsg,"************BB board Power off***********");
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);

    ErrChk( PSU_SetOutputState( g_vi_sInitHandleN5700_1, 0 ));
    
    //Clear DIO state
    ErrChk(USBWriteDigToPort(g_szBscanClearWritePort2Cmd.ucTashName,g_szBscanClearWritePort2Cmd.ucDevport,g_szBscanClearWritePort2Cmd.uiWriteData));

    Delay(5);
    sprintf(szLogMsg,"************BB board Power On and Reset***********");
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    //InitPSU();
    ErrChk(PSU_SetOutputState(g_vi_sInitHandleN5700_1, 1));       
   
    Delay(10);
Error: 

#if(ON_OFF != SIMU_VERS)

    if(iPassFail == PASS) 
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }
    
    CA_DISCARD_OBJ( hStepName );
	CA_FREE( pszStepName );
   
	//If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
    
#else
    testData->result = PASS;
#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                  TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);
    
    return;
			 
}
   

// ************************* 78 character ruler *****************************
//                           End of source file
// **************************************************************************
